package com.maxmlr.music.controller;

import com.maxmlr.music.model.Picture;
import com.maxmlr.music.model.Result;
import com.maxmlr.music.model.User;
import com.maxmlr.music.service.UserService;
import com.maxmlr.music.util.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


/**
 * 用户控制层
 *
 * @author maxiao
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController extends FathController {

    @Autowired
    private UserService userService;

    /**
     * 进入注册页面
     *
     * @param user
     * @return
     */
    @RequestMapping("/toRegister.do")
    public String toRegister() {
        return "page/register";
    }

    /**
     * 进入登录页面
     *
     * @param user
     * @return
     */
    @RequestMapping("/toLogin.do")
    public String toLogin(HttpSession session, Model model) {
        User user = (User) session.getAttribute(USER);
        if (user == null) {
            model.addAttribute(MSG, "请登陆！");
            return "page/login";
        } else {
            model.addAttribute(MSG, "已登陆！");
        }
        return "page/login";
    }

    /**
     * 进入登录
     *
     * @param user
     * @return
     */
    @RequestMapping("/loginFrom.do")
    public String loginFrom(HttpSession session, Model model, String username,
                            String pwd) {
        Map<String, Object> map = userService.checkLogin(username, pwd);
        session.setAttribute(USER, (User) map.get(RET));
        model.addAttribute(USER, (User) map.get(RET));
        if (SUCCESS.equals(Tool.dellNull(map.get(FLAG)))) {
            session.setAttribute(USER, (User) map.get(RET));
            model.addAttribute(USER, (User) map.get(RET));
            return "page/mainPage";
        } else if (NAMEERROR.equals(Tool.dellNull(map.get(FLAG)))) {
            model.addAttribute(MSG, "用户名不存在！");
        } else if (PWDERROR.equals(Tool.dellNull(map.get(FLAG)))) {
            model.addAttribute(MSG, "密码错误！");
        }
        // else if (ISLOGIN.equals(Tool.dellNull(map.get(FLAG)))) {
        // model.addAttribute(MSG, "用户已登陆！");
        // }
        else if (ISFROZEN.equals(Tool.dellNull(map.get(FLAG)))) {
            model.addAttribute(MSG, "用户已冻结！");
        }
        log.info("loginFrom:" + map.toString());
        return "page/mainPage";
//		return "page/login";
    }

    /**
     * 登出
     *
     * @param user
     * @return
     */
    @RequestMapping("/loginOut.do")
    public String loginOut(HttpSession session, Model model) {
        User user = (User) session.getAttribute(USER);
        if (user == null) {
            model.addAttribute(MSG, "未登陆！");
            return "page/login";
        } else {
            Map<String, Object> map = userService.loginOut(user);
            if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
                session.invalidate();
                model.addAttribute(USER, new User());
            } else {
                model.addAttribute(MSG, "登出失败！");
                return "page/mainPage";
            }
        }
        return "page/login";
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @RequestMapping("/registFrom.do")
    public String registFrom(User user, HttpSession session, Model model) {
        log.info("user" + user.toString());
        Map<String, Object> map = userService.saveUser(user);
        if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
            user = (User) map.get(RET);
            session.setAttribute(USER, user);
            model.addAttribute(MSG, "注册成功！");
            return "page/login";
        } else {
            model.addAttribute(MSG, "注册失败！");
        }
        return "page/register";
    }

    /**
     * 加载用户图片
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/toPicture.do")
    public Result toPicture(Integer page) {
        Map<String, Object> map = userService.findUserPic(page);
        Result re = new Result();
        if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
            re.setData((List<Picture>) map.get(RET));
        }
        return re;
    }

    /**
     * 用户名检查
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkUserName.do")
    public Result checkUserName(String username) {
        Map<String, Object> map = userService.findByUserName(username);
        Result re = new Result();
        if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
            re.setData(true);
        } else {
            re.setData(false);
        }
        return re;
    }

    /**
     * 加载主页 图片
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/addmainpic.do")
    public Result addmainpic(Integer page, Integer pagenum) {
        Map<String, Object> map = userService.findMainPic(page, pagenum);
        Result re = new Result();
        if (TRUE.equals(Tool.dellNull(map.get(FLAG)))) {
            re.setData((List<Picture>) map.get(RET));
        }
        return re;
    }
}
